package com.rain.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.shared.Applications;
import com.rain.entity.User;

@RestController
public class UserController {

	@Autowired
	private LoadBalancerClient loadBalancer;
	
	@Autowired
	private RestTemplate rest;
	
	@Autowired
	private EurekaClient client;
	
	@Value("${profile}")
	private String profile;
	
	@GetMapping("/profile")
	public String getProfile() {
		return this.profile;
	}

	@RequestMapping("/userInfo")
	public String home() {
		
		Applications a = client.getApplications();

		ResponseEntity<String> response = rest.getForEntity("http://microservice-provider-bee/user/abc", String.class);

		ServiceInstance instance = loadBalancer.choose("stores");
		
		System.out.println(instance);
		
		return response.getBody();

	}
	
	@GetMapping("test/{id}")
	public String test(@PathVariable String id) {
		return id;
	}
	
	@PostMapping("/post")
	public String post(@RequestBody User user)  {
		
		return user.toString();
	}

}
